// pages/uploadArchives/index.js
import util from "../../utils/util";
var http = require('../../utils/http');
var api = require('../../utils/api');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        calendarConfig: {
            defaultDay: false
        },
        profileid: "",
        date: "",
        hospital: "",
        currentDate: new Date().getTime(),
        minDate: new Date("2022-01-01").getTime(),
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            }
            if (type === 'month') {
                return `${value}月`;
            }
            return value;
        },
        fileList: [],
        showDatePopup: false,
        file_id: "", //病历图片id
        objData: {
            r_yzcd: "",
            l_yzcd: "",
            r_qgdqj: "",
            l_qgdqj: "",
            r_qgdzj: "",
            l_qgdzj: "",
            r_sl: "",
            l_sl: "",
            r_jzsl: "",
            l_jzsl: "",
            r_jmqlk1: "",
            l_jmqlk1: "",
            r_jmqlk2: "",
            l_jmqlk2: "",
            r_yy: "",
            l_yy: "",
        },
        child_id: "",
        mode: "", //当前模式 add 创建  update  编辑
        showQuGuangDuPopup: false,
        showShiLiDuPopup: false,
        key: "",
        columns: [{
                values: ['+', '-']
            },
            {
                values: ['11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
                defaultIndex: 10
            },
            {
                values: ['.']
            },
            {
                values: ["75", "00", "25", '50'],
                defaultIndex: 1
            },
            {
                values: ['DS'],
                disabled: true
            }
        ],
        columnsShili: ['0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1.0', '1.2', '1.5']
    },
    deleteImg(event) {
        let index = event.detail.index
        this.data.fileList.splice(index, 1)
        this.setData({
            fileList: this.data.fileList
        })
    },
    afterRead(event) {
        let _this = this
        const {
            file
        } = event.detail;
        wx.uploadFile({
            url: http.report_host + api.uploadFile, // 仅为示例，非真实的接口地址
            filePath: file.url,
            name: 'file',
            formData: {
                category: "records"
            },
            success(res) {
                // 上传完成需要更新 fileList
                let doc = JSON.parse(res.data)
                // 上传完成需要更新 fileList
                const {
                    fileList = []
                } = _this.data;
                fileList.push({
                    ...file,
                    url: http.report_host + '/getFile/' + doc.data.file_id,
                    file_id: doc.data.file_id
                });
                _this.setData({
                    fileList: fileList,
                });

            },
        });
    },
    onInput(event) {
        this.setData({
            currentDate: event.detail,
        });
    },
    onConfirm(event) {
        this.setData({
            showDatePopup: false,
            date: util.formatTime(new Date(event.detail), 1),
        })
    },
    onCancel() {
        this.setData({
            showDatePopup: false
        })
    },
    showDatePopupFun() {
        this.setData({
            showDatePopup: true
        })
    },
    onCloseDatePopup() {
        this.setData({
            showDatePopup: false,
            showQuGuangDuPopup: false,
            showShiLiDuPopup: false
        })
    },
    onChangeInput(event) {
        let key = event.currentTarget.dataset.key
        let value = event.detail
        value = value.replace(/[^\d\.-]/g, '')
        let objectAttr = 'objData.' + key
        this.setData({
            [objectAttr]: value
        })
    },
    onChangeInput1(event) {
        let key = event.currentTarget.dataset.key
        let value = event.detail
        value = value.replace(/[^\d\.]/g, '')
        let objectAttr = 'objData.' + key
        this.setData({
            [objectAttr]: value
        })
    },
    submitFun() {
        if (this.data.date == '') {
            wx.showModal({
                showCancel:false,
                content: "请选择检测日期",
              })
            return
        }
        if (this.data.hospital == '') {
            wx.showModal({
                showCancel:false,
                content: '请输入检查医院',
              })
            return
        }
        if (this.data.objData.r_yzcd == '' || this.data.objData.l_yzcd == '') {
            wx.showModal({
                showCancel:false,
                content: '请输入眼轴长度',
              })
            return
        }
        if (this.data.objData.r_qgdqj == '' || this.data.objData.l_qgdqj == '') {
           
            wx.showModal({
                showCancel:false,
                content: '请输入球镜',
              })
            return
        }
        // createProFile
        let file_ids = this.data.fileList.map(ele => {
            return ele.file_id
        })
        let obj = {
            child_id: this.data.child_id,
            file_id: file_ids + '',
            hos_name: this.data.hospital,
            profile_time: this.data.date,
            ...this.data.objData
        }
        if (this.data.mode == 'add') {
            http.postHttp(api.createProFile, obj,
                (res) => {
                    if (res.code == 0) {
                      
                        wx.showToast({
                            title: '创建成功',
                          })
                        wx.navigateBack({
                            delta: 1,
                        })
                    }
                },
                (err) => {
                    console.log(err);
                },
            )
        } else {
            // 编辑模式 updateProFile
            http.putHttp(api.updateProFile + this.data.profileid, {
                    child_id: this.data.child_id,
                    file_id: file_ids + '',
                    hos_name: this.data.hospital,
                    profile_time: this.data.date,
                    ...this.data.objData
                },
                (res) => {
                    if (res.code == 0) {
                        wx.showToast({
                            title: '修改成功',
                          })
                        // wx.redirectTo({
                        //     url: '/pages/archivesPage/index',
                        // })
                        wx.navigateBack({
                            delta: 1,
                        })
                    }
                },
                (err) => {
                    console.log(err);
                },
            )
        }

    },
    onClickInput(event) {
        let key = event.currentTarget.dataset.key
        let objAttr = 'columns[4].values'
        this.setData({
            showQuGuangDuPopup: true,
            key: key,
            [objAttr]: key.includes('qgdqj') ? ['DS'] : ['DC']
        })
    },
    onConfirmColumns(e) {
        let data = (e.detail.value);
        let val = data[0] + data[1] + data[2] + data[3]
        let objectAttr = 'objData.' + this.data.key
        this.setData({
            [objectAttr]: val,
            showQuGuangDuPopup: false
        })
    },
    onCancelColumns() {
        this.setData({
            showQuGuangDuPopup: false,
            showShiLiDuPopup: false
        })
    },
    onClickShiliInput(event) {
        let key = event.currentTarget.dataset.key
        this.setData({
            showShiLiDuPopup: true,
            key: key
        })
    },
    onConfirmShiliColumns(e) {
        let data = (e.detail.value);
        let objectAttr = 'objData.' + this.data.key
        this.setData({
            [objectAttr]: data,
            showShiLiDuPopup: false
        })
    },
    onSelectDate(e) {
        let date = e.detail.year + '-' + e.detail.month + '-' + e.detail.day
        this.setData({
            showDatePopup: false,
            date: date
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            child_id: options.child_id,
            mode: options.mode
        })
        if (options.mode == 'update') {
            wx.setNavigationBarTitle({
                title: '编辑修改',
            })
            let info = JSON.parse(options.info)
            let fileList = []
            if (info.file_id) {
                fileList = info.file_id.map(ele => {
                    return {
                        url: http.report_host + '/getFile/' + ele,
                        name: ele + ".png",
                        isImage: true,
                        file_id: ele
                    }
                })
            }
            delete info.yaer
            delete info.status
            info.profile_time = info.profile_time.replace("月", "-")
            info.profile_time = info.profile_time.replace("日", "")
            this.setData({
                profileid: info.id,
                date: info.year + '-' + info.profile_time,
                hospital: info.hos_name,
                id: info.id,
                child_id: info.child_id,
                objData: {
                    r_yzcd: info.r_yzcd,
                    l_yzcd: info.l_yzcd,
                    r_qgdqj: info.r_qgdqj,
                    l_qgdqj: info.l_qgdqj,
                    r_qgdzj: info.r_qgdzj,
                    l_qgdzj: info.l_qgdzj,
                    r_sl: info.r_sl,
                    l_sl: info.l_sl,
                    r_jzsl: info.r_jzsl,
                    l_jzsl: info.l_jzsl,
                    r_jmqlk1: info.r_jmqlk1,
                    l_jmqlk1: info.l_jmqlk1,
                    r_jmqlk2: info.r_jmqlk2,
                    l_jmqlk2: info.l_jmqlk2,
                    r_yy: info.r_yy,
                    l_yy: info.l_yy,
                },
                fileList: fileList
            })

        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})